package com.example.victory.utils;

import com.alibaba.fastjson.JSON;
import com.example.victory.config.ServiceError;
import com.cgws.fintech.sdk.api.inner.crmapi.model.ApiResponseFsdp;
import com.cgws.fintech.sdk.api.inner.crmapi.model.ApiResponseInnerCrmapiStr;
import com.example.victory.exception.GlobalException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

@Slf4j
public class JsonUtils {

    public static <T> T getResultEntityCommon(ResponseEntity<ApiResponseInnerCrmapiStr> fsdpResponseEntity, Class<T> t){
        if (fsdpResponseEntity.getStatusCode() != HttpStatus.OK || fsdpResponseEntity.getBody() == null || fsdpResponseEntity.getBody().getData() == null) {
            log.error("请求CRMAPI错误:resp={}", fsdpResponseEntity.getBody());
            throw new GlobalException(ServiceError.REQUEST_CRMAPI_FAIL);
        }
        try {
            return JSON.parseObject(fsdpResponseEntity.getBody().getData().getResult(),t);
        }catch (Exception e){
            log.error("解析crmapi响应体异常,e={}",e);
            throw new GlobalException(ServiceError.PARSE_FSDP_RESP_ERROR);
        }
    }

    public static <T> T getResultEntity(ResponseEntity<ApiResponseFsdp> fsdpResponseEntity, Class<T> t){
        if (fsdpResponseEntity.getStatusCode() != HttpStatus.OK || fsdpResponseEntity.getBody() == null || fsdpResponseEntity.getBody().getData() == null) {
            log.error("请求CRMAPI错误:resp={}", fsdpResponseEntity.getBody());
            throw new GlobalException(ServiceError.REQUEST_CRMAPI_FAIL);
        }
        try {
            return JSON.parseObject(fsdpResponseEntity.getBody().getData().getResult(),t);
        }catch (Exception e){
            log.error("解析crmapi响应体异常,e={}",e);
            throw new GlobalException(ServiceError.PARSE_FSDP_RESP_ERROR);
        }
    }
}
